<?php include "../../config.php"; ?>

<?



$wechat_detail=select_one_line_all_items($table_prefix."app_mp","nid > 0");


//print_r($wechat_detail);




//配置参数的数组
$CONF =  array(
'__APPID__' =>$wechat_detail['mpapp_id'],
'__SERECT__' =>$wechat_detail['mpapp_secret']
);



if($wechat_detail['refresh_token']<date("YmdHis"))
{


echo $wechat_detail['refresh_token'];
echo "<br/>已经超时<br/>";
echo date("YmdHis");




//过期了就要重新拿,没过期就直接提取可以了。
$gettokenUrl  =  "https://api.weixin.qq.com/cgi-bin/token".
"?grant_type=client_credential".
"&appid=" . $CONF['__APPID__'].
"&secret=".$CONF['__SERECT__']
;


$get_json=file_get_contents($gettokenUrl);


echo  "<br/>";
echo $get_json;
echo  "<br/>";
echo  "<br/>";
$access_token=substr($get_json,17,136);
$token['ai_access_token']=substr($get_json,17,136);
//echo  "<br/>";

echo "现在时间<br/>".$token_expire=date("YmdHis");
echo  "<br/>";
$token_expire=date("YmdHis",strtotime('+1 hour'));
echo $token['ai_refresh_token']=date("YmdHis",strtotime('+1 hour'));
echo  "<br/>";


echo $update_token=update_date($table_prefix."app_mp",$token,"nid",'1');
    $query = do_query($update_token) or die (mysqli_error().$update_token);
    mysqli_close($con);
}


else
{
    echo "<br/>未超时，直接使用<br/>";
    $access_token=$wechat_detail['access_token'];
    echo "<br/>";

}





$_getcontent=file_get_contents("https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token."&next_openid=");


$_contents=json_decode($_getcontent,true);
$_openid=$_contents['data']['openid'];

print_r2($_openid);

//和系统现有的表格对比，只保留未入库的openid
$_wechat_member=select_lines_all_items($table_prefix."wechat_member","","nid >= 0" , "");
//print_r2($_wechat_member);

$_wechat_member_1darray=transform_2darray_in_1darray_number_as_key($_wechat_member,"openid");

//print_r2($_wechat_member_1darray);


//对比之后 新数据-旧数据
$_openid_result=array_diff($_openid,$_wechat_member_1darray);

print_r2($_openid_result);

if(count($_openid_result))
{
$openidstr=implode("'),('",$_openid_result);

echo $sql="INSERT INTO ".$table_prefix."wechat_member (openid) values ('".$openidstr."')";
$result = do_query($sql);
mysqli_close($con);
}



//旧新对比 旧数据-新数据，剩下的就是取消关注了的
$_openid_cancel=array_diff($_wechat_member_1darray,$_openid);

//取消关注了的
echo count($_openid_cancel);
print_r2($_openid_cancel);

$table=$table_prefix."wechat_member";

if(count($_openid_cancel))
{
    $openid_cancel_str=implode("','",$_openid_cancel);

    echo $sql = " DELETE FROM $table WHERE openid in ( '".$openid_cancel_str."')";

    $result = do_query($sql);
    mysqli_close($con);
}

?> 


 